<?php
//*************************************************************************
//* Bali Open License 1.0
//*
//* The contents of this file are from Bali. You can use it in any way
//* only if you keep this header. 
//*
//* http://www.BaliOnWeb.com
//*
//*************************************************************************

function mysql_get_all_unanswered_questions($user)
{
	$q = query(sprintf('SELECT s_id, q_list, asked_by FROM sheets WHERE answered_by = %d  AND done_status = 0', $user));
	
	$results = array();
	
	// maybe multiple rows, so use while here.
	while($row = mysql_fetch_assoc($q))
	{
		$q_id_list = explode("," , $row['q_list']);
		$i = 0;
		foreach($q_id_list as $q_id) {
			$row[$i] = mysql_get_question_by_id($q_id);
			$i++;
		}
		$row['howmany_q'] = $i;
		//debug_print($row);
    	$results[$row['asked_by']] = $row;
  	}
  	return $results;
}

function mysql_get_question_by_id($q_id)
{
	$q = query(sprintf('SELECT q_id, q_content, creator FROM questions WHERE q_id = %d', $q_id));

	$arr = mysql_fetch_array($q);
	$ret = array();
	$ret['q_id'] = $arr[0];
	$ret['q_content'] = stripslashes($arr[1]);
	$ret['creator'] = $arr[2];
	//debug_print($ret);
	return $ret;
}

//	$arr_param['do'] = 'submit' , 'ignore';
//	$arr_param['q_list'] = $_REQUEST['q_list'];
//	$arr_param['s_id'] = $_REQUEST['s_id'];
function mysql_set_answers($arr_param)
{
	if('ignore' == $arr_param['do']) {
		// ignore this sheet, just make a mark and return
		return query(sprintf('UPDATE sheets SET done_status = 2 WHERE s_id = %d', $arr_param['s_id']));			
	} else {
		// make it as answered
		query(sprintf('UPDATE sheets SET done_status = 1 WHERE s_id = %d', $arr_param['s_id']));
		
		// save answers now
		$arr_q_id = explode("," , $arr_param['q_list']);
		
		// create query
		$query_statement = 'INSERT INTO `answers` (`q_id`, `s_id`, `a_content`) VALUES ';
		foreach($arr_q_id as $q_id) {
			$a_tag = 'a_content'.$q_id;
			$a_content = $arr_param[$a_tag];
			$a_content = addslashes($a_content);
			$query_statement = $query_statement . sprintf('(%d, %d, \'%s\')', $q_id, $arr_param['s_id'], $a_content) .',';
		}
		$query_statement[strlen($query_statement)-1] = ';';
		
		// execture this
		query($query_statement);
		
		return $query_statement;
	}
}

///////////////////////////////////////////////////////////////////
function mysql_get_all_restaurants_data()
{
  $q = query(sprintf('SELECT restaurant, name, pic FROM restaurant WHERE 1'));
  $results = array();
  while($row = mysql_fetch_assoc($q))
  {
    $row['name'] = stripslashes($row['name']);
    $row['id'] = $row['restaurant'];
    $results[$row['restaurant']] = $row;
  }
  return $results;
}

function mysql_restaurant_get_users($restaurant)
{
  $q = query(sprintf('SELECT user FROM restaurant_user WHERE restaurant = %d ORDER BY time DESC LIMIT 100', $restaurant));

  $ret = array();
  while(list($id) = mysql_fetch_array($q))
    $ret[$id] = $id;

  return $ret;
}

function mysql_user_get_restaurants($user)
{
  $q = query(sprintf('SELECT restaurant FROM restaurant_user WHERE user = %d ORDER BY time DESC', $user));

  $ret = array();

  while(list($id) = mysql_fetch_array($q))
    $ret[$id] = $id;

  return $ret;
}

function mysql_user_rate_restaurant($user, $restaurant, $rating)
{
  return query(sprintf('REPLACE INTO restaurant_user (user, restaurant, review_score) VALUES (%d, %d, %d)', $user, $restaurant, $rating));
}

function mysql_user_remove_restaurant($user, $restaurant)
{
  return query(sprintf('DELETE FROM restaurant_user WHERE restaurant = %d AND user = %d', $restaurant, $user));
}

function mysql_user_get_restaurant_rating($user, $restaurant)
{
  $q = query(sprintf('SELECT review_score from restaurant_user WHERE user = %d AND restaurant = %d', $user, $restaurant));
  if ($row = mysql_fetch_assoc($q)) {
    return $row['review_score'];
  } else {
    return 0;
  }
}

function mysql_get_restaurants_from_friends($friends_array)
{
  $sql_list = '(' . implode(',', $friends_array) . ')';
  $q = query(sprintf('SELECT restaurant, count(user) as count FROM restaurant_user WHERE user IN '.$sql_list . ' GROUP BY restaurant ORDER BY count DESC LIMIT 10'));
  $rests = array();
  while ($row = mysql_fetch_assoc($q))
  {
    $rests[$row['restaurant']] = $row['count'];
  }
  return $rests;
}

function query($sql)
{
    global $conn;
    mysql_query("set names utf8");
    return mysql_query($sql, $conn);
}

?>
